package com.clt.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.clt.web.service.IWxMenuService;

/**
 * 微信菜单的controller
 * 
 * @author wenjian
 * 
 */
@Controller
@RequestMapping("/weixin/menu")
public class WeixinMenuController {

	private final Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	private IWxMenuService wxMenuService;

	/**
	 * 新建菜单
	 * 
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping("/save")
	public ModelAndView save(HttpServletRequest req, HttpServletResponse resp) {
		ModelAndView mav = new ModelAndView("mobile/menu");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 同步菜单到微信方法
			Map<String, Object> result = wxMenuService.synWxMember(req);
			if((Boolean) result.get("status")){
				// 新增完成后查找菜单
				map = wxMenuService.findMenuInfo();
				mav.addObject("status", "新增成功");
				mav.addObject("message", "");
			}else{
				mav.addObject("status", "新增失败");
				mav.addObject("message", result.get("msg"));
			}
		} catch (Exception e) {
			mav.addObject("status", "新增失败");
			mav.addObject("message", e.getMessage());
			log.info("同步菜单失败,原因{}", e.getMessage());
		}
		mav.addObject("map", map);
		return mav;
	}

	/**
	 * 查看菜单
	 * 
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping("/find")
	public ModelAndView find(HttpServletRequest req, HttpServletResponse resp) {
		ModelAndView mav = new ModelAndView("mobile/menu");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = wxMenuService.findMenuInfo();
			mav.addObject("status", "查找成功");
			mav.addObject("message", "");
		} catch (Exception e) {
			mav.addObject("status", "查找失败");
			mav.addObject("message", e.getMessage());
			log.info("菜单查找失败,原因{}", e.getMessage());
		}
		mav.addObject("map", map);
		return mav;
	}
}
